package com.jdb.banner.controller.mixins;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.jdb.banner.api.dto.BannerDTO;

import java.io.*;
import java.util.Objects;

public class BannerMetaSerializer extends JsonSerializer<BannerDTO> {
    @Override
    public void serialize(BannerDTO map, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {

        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("title", map.getTitle());
        jsonGenerator.writeStringField("cover", map.getCover());
        jsonGenerator.writeStringField("summary", map.getSummary());
        jsonGenerator.writeObjectField("category", map.getCategory());
        if(Objects.nonNull(map.getCategory()) && Objects.nonNull(map.getMeta())) {
            switch (map.getCategory()) {
                case LOAN:
                    jsonGenerator.writeObjectField("loanId", map.getMeta().get("id"));
                    break;
                default:
                    break;
            }
        }
        jsonGenerator.writeEndObject();
    }
}
